var form,$,table,layer,upload;
var user = JSON.parse(localStorage.getItem("user"));
var userId = user.userId;
layui.use(['form','jquery','table','layer','upload'],function() {
    form = layui.form;
    $ = layui.jquery;
    table = layui.table;
    layer = layui.layer;
    upload = layui.upload;

    //查询个人微圈信息
    getMyMicrocircleData('','',userId);

    //table点击事件，存储数据
    table.on('row(test)',function (data) {
        localStorage.setItem("microCircleData",JSON.stringify(data.data));
    });
});

function getMyMicrocircleData(artcleTitle,createDate,userId) {
    $.ajax({
        url: "/microcircle/getMyMicrocircleList",
        type: "post",
        dataType: "json",
        data: {
            'artcleTitle': artcleTitle,
            'createDate': createDate,
            'userId': userId
        },
        success: function (data) {
            showMyMicrocircleTable(data.data);
        }

    });
}

//展示个人微圈列表
function showMyMicrocircleTable(data) {
    table.render({
        elem: "#demo",
        data: data,
        page: true,
        limit: 10,
        cols: [[
            {checkbox:true},
            {field:'microcircleId',title:'序号',align:'center',height:'20px',unresize:true},
            {field:'trueName',title:'用户昵称',align:'center',unresize:true},
            {field:'artcleTitle',title:'文章标题',align:'center',unresize:true},
            {field:'createDate',title:'创建时间',width:170,align:'center',unresize:true},
            {field:'account',title:'用户账号',align:'center',unresize:true},
            {field:'acticleLike',title:'点赞数量',align:'center',unresize:true},
            {field:'collectionNum',title:'收藏数量',align:'center',unresize:true,templet:function (data) {
                    var collectionNum = 0
                    $.ajax({
                        url:'/microcircle/getMyMicrocircleCollectionNums',
                        type: "post",
                        dataType:'json',
                        async: false,
                        data: {"microcircleId": data.microcircleId },
                        success:function (data) {
                            if (data.data != null){
                                collectionNum = data.data;
                            } else{
                                collectionNum = 0;
                            }
                        }
                    });
                    localStorage.setItem("collectionNum",collectionNum);
                    return collectionNum;
                }},
            {field:'acticleReviews',title:'评论数量',align:'center',unresize:true},
            {field:'acticleShare',title:'分享数量',align:'center',unresize:true},
            {field:'',title:'操作',align:'center',width:100,unresize:true,templet:function(data){
                    return "<a class='operation' onclick='seeMicroCircle("+data.microcircleId+")'>查看</a>&nbsp&nbsp"+
                        "<a onclick='delMyMicroCircle(\""+data.microcircleId+"\")' class='operation'>删除</a>";
                }},
        ]]
    });
}

//搜索
function search() {
    var formData = form.val("formTest");
    getMyMicrocircleData(formData.artcleTitle,formData.createDate,userId);
}

//删除
function delMyMicroCircle(microcircleId) {
    layer.open({
        type: 1,
        title: "删除微圈",
        content: "<div style='font-size: 16px; margin:30px  ;'>您确定要删除此微圈吗?</div>",
        area:['300px','200px'],
        btn: ['确定','取消'],
        yes: function (index, layero) {
            $.ajax({
                url: "/microcircle/delMicroCircle",
                dataType: "json",
                type: "post",
                data: {"microcircleId":microcircleId},
                success: function (data) {
                    getMyMicrocircleData('','',userId);
                    layer.msg("删除成功");
                    layer.close(index);
                }
            });
        }

    });
}

//查看
function seeMicroCircle(microcircleId) {
    layer.open({
        type: 2,
        title: "管理微圈",
        area: ['1000px','550px'],
        content: "/microCircle/seeMicroCircle.html",
        success: function (layero,index) {
            var microCircleData = JSON.parse(localStorage.getItem("microCircleData"));
            console.log(microCircleData);
            var objData = layero.find("iframe").contents();
            //头像
            objData.find("#headerImage").attr("src",microCircleData.acatarUrl);
            //标题
            objData.find("#span_title").text(microCircleData.artcleTitle);
            //昵称
            objData.find("#p_name").text(microCircleData.trueName);
            //标题（右）
            objData.find("#titleid").text(microCircleData.artcleTitle);
            //创作人
            objData.find("#span_user").text(microCircleData.trueName);
            //日期
            objData.find("#span_date").text(microCircleData.releaseDate);
            //分类
            queryMicroCircleClass(objData,microCircleData.acticleCategroyId);
            //账号
            objData.find("#span_id").text(microCircleData.account);
            //内容
            objData.find("#text_id").html(microCircleData.article);
            //用户名
            objData.find("input[name=userName]").val(user.trueName);
            //账号
            objData.find("input[name=userNum]").val(user.account);
        }
    });
}

function queryMicroCircleClass(objData,acticleCategroyId) {
    $.ajax({
        url: "/microcircle/queryMicroCircleClass",
        type: "post",
        dataType: "json",
        data: {"acticleCategroyId": acticleCategroyId},
        success: function (item) {
            if (item.data != null){
                objData.find("#span_fenlei").text(item.data.artcleCategroyName);
            }
        }
    });
}

//刷新
function refresh() {
    location.reload();
}

//返回首页
function backOff(){
    parent.jumpSystemHome();
}